package com.ali.juejin.bind

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import androidx.databinding.BindingAdapter
import com.ali.juejin.widget.ReplyTextView
import com.blankj.utilcode.util.LogUtils

object BindReplyTextView {

    @BindingAdapter("replyUser","user","isAuthor","replyLevel","userLevel","content")
    @JvmStatic
    fun loadReply(view: ReplyTextView,replyUser:String, user:String,isAuthor:Boolean,replyLevel:Int,userLevel:Int,content:String){

        val span = SpannableStringBuilder()
        // 为回复当前回答者
        val userLvBackgroundColorSpanStyle = BackgroundColorSpan(Color.parseColor("#6ECEFF"))
        val userLvColorSpanStyle = ForegroundColorSpan(Color.parseColor("#ffffff"))
        val replyLvBackgroundColorSpanStyle = BackgroundColorSpan(Color.parseColor("#6ECEFF"))
        val replyLvColorSpanStyle = ForegroundColorSpan(Color.parseColor("#ffffff"))
        val replyUserNameStyle = ForegroundColorSpan(Color.parseColor("#38383B"))
        val replyUserNameSize = AbsoluteSizeSpan(16,true)
        val userNameStyle = ForegroundColorSpan(Color.parseColor("#38383B"))
        val userNameSize = AbsoluteSizeSpan(16,true)
        var replyUserName = replyUser
        var userName = user
        val userLevelStr = userLevel.toString()
        val replyLevelStr = replyLevel.toString()

        if(isAuthor){
            userName += "(作者)"
        }


//        LogUtils.wTag("是否是第一次回复",firstComment)
//        LogUtils.wTag("回复id",replyId)
        if (replyUserName.isEmpty()){
            val str = userName+"lv"+userLevel+" : "+content
//                LogUtils.wTag("回复（只有回复人）",str)
            span.append(str)
            // user
            span.setSpan(userNameStyle,0,userName.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            span.setSpan(userNameSize,0,userName.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            span.setSpan(userLvBackgroundColorSpanStyle,userName.length,userName.length+2+userLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            span.setSpan(userLvColorSpanStyle,userName.length,userName.length+2+userLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            span.setSpan(userLvBackgroundColorSpanStyle,userName.length,userName.length+2+userLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            span.setSpan(userLvColorSpanStyle,userName.length,userName.length+2+userLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            view.setText(span)
            return
        }
        val str = userName+"lv"+userLevel+"回复"+replyUserName+"lv"+replyLevel+" : "+content
//        LogUtils.wTag("回复（所有人）",str)
        span.append(str)
        // reply_user

        span.setSpan(replyUserNameStyle,0,userName.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(replyUserNameSize,0,userName.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        // user
        span.setSpan(userNameStyle,userName.length+4+userLevelStr.length,replyUserName.length+4+userName.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(userNameSize,userName.length+4+userLevelStr.length,replyUserName.length+4+userName.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        // lv
        span.setSpan(userLvBackgroundColorSpanStyle,userName.length,userName.length+2+userLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(userLvColorSpanStyle,userName.length,userName.length+2+userLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(userLvBackgroundColorSpanStyle,userName.length,userName.length+2+userLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(userLvColorSpanStyle,userName.length,userName.length+2+userLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)


        span.setSpan(replyLvBackgroundColorSpanStyle,userName.length+4+userLevelStr.length+replyUserName.length,userName.length+6+userLevelStr.length+replyUserName.length+replyLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(replyLvColorSpanStyle,userName.length+4+userLevelStr.length+replyUserName.length,userName.length+6+userLevelStr.length+replyUserName.length+replyLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(replyLvBackgroundColorSpanStyle,userName.length+4+userLevelStr.length+replyUserName.length,userName.length+6+userLevelStr.length+replyUserName.length+replyLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(replyLvColorSpanStyle,userName.length+4+userLevelStr.length+replyUserName.length,userName.length+6+userLevelStr.length+replyUserName.length+replyLevelStr.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        // 回复
//                span.setSpan(nameStyle,replyUserName.length,replyUserName.length+2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//                span.setSpan(nameSize,replyUserName.length,replyUserName.length+2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        view.setText(span)
    }
}